#更改xml中某一类
import os
input_dir='/home/xys/CloundShiProjects/traffic_light/trafficlight_dect/初data/sds/'
shu=0
# new_name1 = 'anquandai'
# new_name2 = 'e_fuzhuzhidong'
# new_name3 = 'e_abs'
# new_name4 = 'e_asr'
# new_name5='d'

import xml.etree.ElementTree as ET

for filename in os.listdir(input_dir):
    file_front = filename.split('.')[0]
    file_path = os.path.join(input_dir, filename)
    dom = ET.parse(file_path)
    root = dom.getroot()
    x = root.find('filename').text
    y = x.split('.')[1]
    if y not in ['jpg']:
        print(y)
        root.find('filename').text = file_front+".jpg"
        shu = shu + 1

#     for obj in root.iter('annotation'):  # 获取object节点中的name子节点
#         # if obj.find('name').text == 'anquandai2rd':
#         obj.find('filename').text = file_front
#         shu=shu+1
#             #print("change %s to %s." % (yuan_name, new_name1))
#         # elif obj.find('name').text == 'fuzhuzhidong':
#         #     obj.find('name').text = new_name2
#         #     shu = shu + 1
#         # elif obj.find('name').text == 'abs':
#         #     obj.find('name').text = new_name3
#         #     shu = shu + 1
#         # elif obj.find('name').text == 'asr':
#         #     obj.find('name').text = new_name4
#         #     shu = shu + 1
#         # elif obj.find('name').text == 'fanmian':
#         #     obj.find('name').text = new_name4
#         #     shu = shu + 1
#
  # 保存到指定文件
    dom.write(file_path, xml_declaration=True)
print("有%d个文件被成功修改" % shu)
